import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class FilledRectangleTool extends Tool {

	protected Point startingMousePosition;
	protected Point currentMousePosition;
	protected Color saveColor;

	public FilledRectangleTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
		super(canvas, ShapeObjects, mdUser);
	}

	protected void drawFilledRectangle(int x, int y, int width, int height) {
		int rectX = x;
		int rectY = y;
		int rectWidth = width;
		int rectHeight = height;
		if (width < 0) {
			rectX = x + width;
			rectWidth = -width;
		} 
		if (height < 0) {
			rectY = y + height;
			rectHeight = -height;
		}
		Graphics iBGraphics = canvas.getImageBufferGraphics();
		iBGraphics.fillRect(rectX, rectY, rectWidth, rectHeight);

		canvas.repaint();
	}

	public void mousePressed(MouseEvent e)  {
		/* Establish starting point for next drawing */
		startingMousePosition = e.getPoint();
		currentMousePosition = startingMousePosition;
		Graphics iBGraphics = canvas.getImageBufferGraphics();
		saveColor = iBGraphics.getColor( );
		iBGraphics.setXORMode(Color.lightGray);
		iBGraphics.setColor(Color.white);
		drawFilledRectangle(startingMousePosition.x,
				startingMousePosition.y, 1,1);

	}

	public void mouseDragged(MouseEvent e)  {
		Point newMousePosition = e.getPoint();
		drawFilledRectangle(startingMousePosition.x, startingMousePosition.y,
				currentMousePosition.x -startingMousePosition.x +1,
				currentMousePosition.y - startingMousePosition.y +1);
		drawFilledRectangle(startingMousePosition.x,startingMousePosition.y,
				newMousePosition.x -startingMousePosition.x + 1,
				newMousePosition.y- startingMousePosition.y + 1);
		currentMousePosition = newMousePosition;
	}


	public void mouseReleased(MouseEvent e) { 
		Point newMousePosition = e.getPoint();
		drawFilledRectangle(startingMousePosition.x,startingMousePosition.y,
				newMousePosition.x -startingMousePosition.x + 1,
				newMousePosition.y- startingMousePosition.y + 1);
		Graphics iBGraphics = canvas.getImageBufferGraphics();
		ShapeObjects.add(new FilledRectangleObject(startingMousePosition, currentMousePosition, saveColor));	
		iBGraphics.setPaintMode();
		paintObjects(0);
		iBGraphics.setColor(saveColor);
	}
	public void keyPressed(KeyEvent e) {}

}
